package com.tjec.project.system.service.impl;

import com.tjec.common.constant.Constants;
import com.tjec.common.constant.UserConstants;
import com.tjec.common.exception.CustomException;
import com.tjec.common.exception.user.CaptchaException;
import com.tjec.common.utils.MessageUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.aspectj.lang.annotation.DataScope;
import com.tjec.framework.manager.AsyncManager;
import com.tjec.framework.manager.factory.AsyncFactory;
import com.tjec.framework.redis.RedisCache;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.esb.service.IPushToPortalUserInfoService;
import com.tjec.project.mdmSynchronous.api.JSONUtils;
import com.tjec.project.mdmSynchronous.api.ThreadPost;
import com.tjec.project.mdmSynchronous.domain.BmsProjectLeader;
import com.tjec.project.mdmSynchronous.mapper.BmsProjectLeaderMapper;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import com.tjec.project.system.domain.*;
import com.tjec.project.system.mapper.*;
import com.tjec.project.system.service.ISysConfigService;
import com.tjec.project.system.service.ISysUserService;

import ch.qos.logback.core.status.Status;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户 业务层处理
 *
 * @author tjec
 */
@Service
@Configuration
public class SysUserServiceImpl implements ISysUserService {
	private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

	@Autowired
	private SysUserMapper userMapper;

	@Autowired
	private SysRoleMapper roleMapper;

	@Autowired
	private SysPostMapper postMapper;

	@Autowired
	private SysUserRoleMapper userRoleMapper;

	@Autowired
	private SysUserPostMapper userPostMapper;

	@Autowired
	private ISysConfigService configService;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Autowired
	private BmsUserBusinessMapper bmsUserBusinessMapper;

	@Autowired
	private BmsProjectLeaderMapper bmsProjectLeaderMapper;

	@Autowired
	private IBmsLeaderMemberService bmsLeaderMemberService;
	
	@Value("${h3bpm.modifyUserStatus}")
    private  String url;
	
	@Value("${h3bpm.baseUrl}")
    private  String baseUrl;
	
	@Value("${h3bpm.cancelInstanceUrl}")
	private String cancelInstanceUrl;
	
	@Autowired
    private IPushToPortalUserInfoService pushToPortalUserInfoService;
	
	@Value("${PORTAL.baseUrl}")
    private  String portalBaseUrl;
	
	@Value("${PORTAL.userStatusUrl}")
    private  String userStatusUrl;

	/**
	 * 根据条件分页查询用户列表
	 *
	 * @param user 用户信息
	 * @return 用户信息集合信息
	 */
	@Override
	@DataScope(deptAlias = "d", userAlias = "u")
	public List<SysUser> selectUserList(SysUser user) {
		return userMapper.selectUserList(user);
	}

	@Override
	public List<SysUser> getUserByGuids(String[] guids) {
		return userMapper.getUserByGuids(guids);
	}

	/**
	 * 通过用户名查询用户
	 *
	 * @param userName 用户名
	 * @return 用户对象信息
	 */
	@Override
	public SysUser selectUserByUserName(String userName) {
		return userMapper.selectUserByUserName(userName);
	}

	@Override
	public SysUser selectUserByPhonenumber(String phonenumber) {
		return userMapper.selectUserByPhonenumber(phonenumber);
	}

	@Override
	public SysUser selectUserByUser(SysUser sysUser) {
		return userMapper.selectUserByUser(sysUser);
	}

	/**
	 * 通过用户ID查询用户
	 *
	 * @param userId 用户ID
	 * @return 用户对象信息
	 */
	@Override
	public SysUser selectUserById(String userId) {
		return userMapper.selectUserById(userId);
	}

	/**
	 * 查询用户所属角色组
	 *
	 * @param userName 用户名
	 * @return 结果
	 */
	@Override
	public String selectUserRoleGroup(String userName) {
		List<SysRole> list = roleMapper.selectRolesByUserName(userName);
		StringBuffer idsStr = new StringBuffer();
		for (SysRole role : list) {
			idsStr.append(role.getRoleName()).append(",");
		}
		if (StringUtils.isNotEmpty(idsStr.toString())) {
			return idsStr.substring(0, idsStr.length() - 1);
		}
		return idsStr.toString();
	}

	/**
	 * 查询用户所属岗位组
	 *
	 * @param userName 用户名
	 * @return 结果
	 */
	@Override
	public String selectUserPostGroup(String userName) {
		List<SysPost> list = postMapper.selectPostsByUserName(userName);
		StringBuffer idsStr = new StringBuffer();
		for (SysPost post : list) {
			idsStr.append(post.getPostName()).append(",");
		}
		if (StringUtils.isNotEmpty(idsStr.toString())) {
			return idsStr.substring(0, idsStr.length() - 1);
		}
		return idsStr.toString();
	}

	/**
	 * 校验用户名称是否唯一
	 *
	 * @param userName 用户名称
	 * @return 结果
	 */
	@Override
	public String checkUserNameUnique(String userName) {
		int count = userMapper.checkUserNameUnique(userName);
		if (count > 0) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}
	
	/**
	 * 校验用户名称是否唯一
	 *
	 * @param userName 用户名称
	 * @return 结果
	 */
	@Override
	public int checkUserNameUniqueEdit(String userName) {
		int count = userMapper.checkUserNameUniqueEdit(userName);
		return count;
	}

	/**
	 * 校验用户名称是否唯一
	 *
	 * @param user 用户信息
	 * @return
	 */
	@Override
	public String checkPhoneUnique(SysUser user) {
		String userId = StringUtils.isNull(user.getUserId()) ? "-1L" : user.getUserId();
		SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
		if (StringUtils.isNotNull(info) && !userId.equals(info.getUserId())) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	/**
	 * 校验email是否唯一
	 *
	 * @param user 用户信息
	 * @return
	 */
	@Override
	public String checkEmailUnique(SysUser user) {
		String userId = StringUtils.isNull(user.getUserId()) ? "-1L" : user.getUserId();
		SysUser info = userMapper.checkEmailUnique(user.getEmail());
		if (StringUtils.isNotNull(info) && !userId.equals(info.getUserId())) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	/**
	 * 校验用户是否允许操作
	 *
	 * @param user 用户信息
	 */
	public void checkUserAllowed(SysUser user) {
		if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
			throw new CustomException("不允许操作超级管理员用户");
		}
	}

	/**
	 * 新增保存用户信息
	 *
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int insertUser(SysUser user) {

		List<BmsProjectLeader> result = bmsProjectLeaderMapper.selectBmsProjectLeaderByUserglobalId(user.getGlobalId());
		if (!CollectionUtils.isEmpty(result)) {
			SysRole role = roleMapper.checkRoleKeyUnique("project_leader");
			String[] roleIds = user.getRoleIds();
            int size = 0;
            if(roleIds!=null&&roleIds.length>0){
                size = roleIds.length;
            }
			String[] tmp = new String[size + 1];
			for (int i = 0; i < size; i++) {
				tmp[i] = roleIds[i];
			}
			tmp[size] = role.getRoleId();

			user.setRoleIds(tmp);
		}

		String updateBy = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
		// 新增用户信息
		int rows = userMapper.insertUser(user);
		updateBpmUserStatus(user,"1");
		user.setUserId(user.getGuid());
		// 新增用户岗位关联
		insertUserPost(user);
		// 新增用户与角色管理,默认赋予用户普通角色权利
		insertUserRole(user);
		//如果为项目负责人，添加自己到人员清单表中
		if (!CollectionUtils.isEmpty(result)) {
			BmsLeaderMember newBmsLeaderMember = new BmsLeaderMember();
			newBmsLeaderMember.setLeader(user.getGuid());
			newBmsLeaderMember.setUserId(user.getGuid());
			List<BmsLeaderMember> bmsLeaderMembers = bmsLeaderMemberService.selectBmsLeaderMemberList(newBmsLeaderMember);
			if (CollectionUtils.isEmpty(bmsLeaderMembers)) {
				bmsLeaderMemberService.insertBmsLeaderMember(newBmsLeaderMember);
			}
		}
		// 部门技术负责人-组织部门
		String[] deptIds = user.getDeptIds();
		if (deptIds != null && deptIds.length > 0) {
			for (int i = 0; i < deptIds.length; i++) {
				Set<String> techLeaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
				techLeaderSet.addAll(Arrays.asList(dept.getTechLeader().trim().split(",")));
				techLeaderSet.add(user.getUserId());
				dept.setTechLeader(String.join(",", techLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 所技术负责人-组织部门
		String[] instituteDeptIds = user.getInstituteDeptIds();
		if (instituteDeptIds != null && instituteDeptIds.length > 0) {
			for (int i = 0; i < instituteDeptIds.length; i++) {
				Set<String> techLeaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
				techLeaderSet.addAll(Arrays.asList(dept.getWhatBusinessTechLeader().trim().split(",")));
				techLeaderSet.add(user.getUserId());
				dept.setWhatBusinessTechLeader(String.join(",", techLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 专业技术负责人--业务分管类别
		String[] businessTypeIds = user.getBusinessTypeIds();
		if (businessTypeIds != null && businessTypeIds.length > 0) {
			List<BmsUserBusiness> bmsUserBusinesss = new ArrayList<BmsUserBusiness>();
			for (int i = 0; i < businessTypeIds.length; i++) {
				BmsUserBusiness bmsUserBusiness = new BmsUserBusiness();
				bmsUserBusiness.setUserId(user.getUserId());
				bmsUserBusiness.setBusinessId(businessTypeIds[i]);
				bmsUserBusinesss.add(bmsUserBusiness);
			}
			bmsUserBusinessMapper.batchUserBusiness(bmsUserBusinesss);
		}

		// 公章管理员-组织部门
		String[] cachetDeptIds = user.getCachetDeptIds();
		if (cachetDeptIds != null && cachetDeptIds.length > 0) {
			for (int i = 0; i < cachetDeptIds.length; i++) {
				Set<String> cachetManageSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(cachetDeptIds[i]);
				cachetManageSet.addAll(Arrays.asList(dept.getCachetManage().trim().split(",")));
				cachetManageSet.add(user.getUserId());
				dept.setCachetManage(String.join(",", cachetManageSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 业务分管副总-组织部门
		String[] businessDeptIds = user.getBusinessDeptIds();
		if (businessDeptIds != null && businessDeptIds.length > 0) {
			for (int i = 0; i < businessDeptIds.length; i++) {
				Set<String> deputyDirSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(businessDeptIds[i]);
				deputyDirSet.addAll(Arrays.asList(dept.getDeputyDir().trim().split(",")));
				deputyDirSet.add(user.getUserId());
				dept.setDeputyDir((String.join(",", deputyDirSet)));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 部领导-组织部门
		String[] deptLeaderIds = user.getDeptLeaderIds();
		if (deptLeaderIds != null && deptLeaderIds.length > 0) {
			for (int i = 0; i < deptLeaderIds.length; i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
				leaderSet.addAll(Arrays.asList(dept.getLeader().trim().split(",")));
				leaderSet.add(user.getUserId());
				dept.setLeader(((String.join(",", leaderSet))));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 所领导-组织部门
		String[] deptInstituteLeaderIds = user.getDeptInstituteLeaderIds();
		if (deptInstituteLeaderIds != null && deptInstituteLeaderIds.length > 0) {
			for (int i = 0; i < deptInstituteLeaderIds.length; i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
				leaderSet.addAll(Arrays.asList(dept.getWhatBusinessLeader().trim().split(",")));
				leaderSet.add(user.getUserId());
				dept.setWhatBusinessLeader(((String.join(",", leaderSet))));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}
		
		//推送门户
		ToPortalUserInfo toPortalUserInfo = new ToPortalUserInfo();
		toPortalUserInfo.setFlag("1");
		toPortalUserInfo.setUserNo(user.getJobNo());
		toPortalUserInfo.setUserName(user.getNickName());
		toPortalUserInfo.setCompetentOrg(user.getPersonnelDept());
		if(StringUtils.isNotBlank(user.getStatus())) {
			String status = user.getStatus();
			if("0".equals(status)) {
				toPortalUserInfo.setUserStatus("1");
			}else if("1".equals(status)) {
				toPortalUserInfo.setUserStatus("2");
			}else if("2".equals(status)) {
				toPortalUserInfo.setUserStatus("3");
			}
		}
		toPortalUserInfo.setLoginName(user.getUserName());
		pushToPortalUserInfoService.sendUserInfo(toPortalUserInfo);
		return rows;
	}

	/**
	 * 修改保存用户信息
	 *
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	// @Transactional(isolation = Isolation.DEFAULT, propagation =
	// Propagation.REQUIRED, rollbackFor = Exception.class)
	public int updateUser(SysUser user, boolean systemUpdateFlag) {
		String updateBy = "";
		if(!systemUpdateFlag){
			updateBy = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
		}
		String userId = user.getUserId();
		// 删除用户与角色关联
		userRoleMapper.deleteUserRoleByUserId(userId);
		// 新增用户与角色管理
		insertUserRole(user);

		// 部技术负责人-组织部门
		String[] deptIds = user.getDeptIds();

		SysDept sysDept1 = new SysDept();
		sysDept1.setTechLeader(userId);
		List<SysDept> resList1 = sysDeptMapper.selectDeptList(sysDept1);

		if (resList1 == null || resList1.size() == 0) {

			if (deptIds != null && deptIds.length > 0) {
				for (int i = 0; i < deptIds.length; i++) {
					Set<String> techLeaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
					if (StringUtils.isNotBlank(dept.getTechLeader())) {
						techLeaderSet.addAll(Arrays.asList(dept.getTechLeader().trim().split(",")));
					}
					techLeaderSet.add(user.getUserId());
					dept.setTechLeader(String.join(",", techLeaderSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}

		} else {
			for (int i = 0; i < resList1.size(); i++) {
				Set<String> techLeaderSet = new HashSet<>();
				SysDept dept = resList1.get(i);
				if (StringUtils.isNotBlank(dept.getTechLeader())) {
					techLeaderSet.addAll(Arrays.asList(dept.getTechLeader().trim().split(",")));
				}

				techLeaderSet.remove(userId);
				dept.setTechLeader(String.join(",", techLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (deptIds != null && deptIds.length > 0) {
				for (int i = 0; i < deptIds.length; i++) {
					Set<String> techLeaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
					if (StringUtils.isNotBlank(dept.getTechLeader())) {
						techLeaderSet.addAll(Arrays.asList(dept.getTechLeader().trim().split(",")));
					}

					techLeaderSet.add(userId);
					dept.setTechLeader(String.join(",", techLeaderSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}

		// 所技术负责人-组织部门
		String[] instituteDeptIds = user.getInstituteDeptIds();

		SysDept sysDept11 = new SysDept();
		sysDept11.setWhatBusinessTechLeader(userId);
		List<SysDept> resList11 = sysDeptMapper.selectDeptList(sysDept11);

		if (resList11 == null || resList11.size() == 0) {

			if (instituteDeptIds != null && instituteDeptIds.length > 0) {
				for (int i = 0; i < instituteDeptIds.length; i++) {
					Set<String> businessTechLeaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(instituteDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getWhatBusinessTechLeader())) {
						businessTechLeaderSet.addAll(Arrays.asList(dept.getWhatBusinessTechLeader().trim().split(",")));
					}
					businessTechLeaderSet.add(user.getUserId());
					dept.setWhatBusinessTechLeader(String.join(",", businessTechLeaderSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}

		} else {
			for (int i = 0; i < resList11.size(); i++) {
				Set<String> businessTechLeaderSet = new HashSet<>();
				SysDept dept = resList11.get(i);
				if (StringUtils.isNotBlank(dept.getWhatBusinessTechLeader())) {
					businessTechLeaderSet.addAll(Arrays.asList(dept.getWhatBusinessTechLeader().trim().split(",")));
				}

				businessTechLeaderSet.remove(userId);
				dept.setWhatBusinessTechLeader(String.join(",", businessTechLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (instituteDeptIds != null && instituteDeptIds.length > 0) {
				for (int i = 0; i < instituteDeptIds.length; i++) {
					Set<String> techLeaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(instituteDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getWhatBusinessTechLeader())) {
						techLeaderSet.addAll(Arrays.asList(dept.getWhatBusinessTechLeader().trim().split(",")));
					}

					techLeaderSet.add(userId);
					dept.setWhatBusinessTechLeader(String.join(",", techLeaderSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}

		// 专业技术负责人--业务分管类别
		String[] businessTypeIds = user.getBusinessTypeIds();
		bmsUserBusinessMapper.deleteUserBusinessByUserId(userId);
		if (businessTypeIds != null && businessTypeIds.length > 0) {
			List<BmsUserBusiness> bmsUserBusinesss = new ArrayList<BmsUserBusiness>();
			for (int i = 0; i < businessTypeIds.length; i++) {
				BmsUserBusiness bmsUserBusiness = new BmsUserBusiness();
				bmsUserBusiness.setUserId(user.getUserId());
				bmsUserBusiness.setBusinessId(businessTypeIds[i]);
				bmsUserBusinesss.add(bmsUserBusiness);
			}
			bmsUserBusinessMapper.batchUserBusiness(bmsUserBusinesss);
		}

		// 公章管理员-组织部门
		String[] cachetDeptIds = user.getCachetDeptIds();
		SysDept sysDept2 = new SysDept();
		sysDept2.setCachetManage(userId);
		List<SysDept> resList2 = sysDeptMapper.selectDeptList(sysDept2);

		if (resList2 == null || resList2.size() == 0) {
			if (cachetDeptIds != null && cachetDeptIds.length > 0) {
				for (int i = 0; i < cachetDeptIds.length; i++) {
					Set<String> cachetManageSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(cachetDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getCachetManage())) {
						cachetManageSet.addAll(Arrays.asList(dept.getCachetManage().trim().split(",")));
					}

					cachetManageSet.add(user.getUserId());
					dept.setCachetManage(String.join(",", cachetManageSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		} else {

			for (int i = 0; i < resList2.size(); i++) {
				Set<String> cachetManageSet = new HashSet<>();
				SysDept dept = resList2.get(i);
				if (StringUtils.isNotBlank(dept.getCachetManage())) {
					cachetManageSet.addAll(Arrays.asList(dept.getCachetManage().trim().split(",")));
				}

				cachetManageSet.remove(userId);
				dept.setCachetManage(String.join(",", cachetManageSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (cachetDeptIds != null && cachetDeptIds.length > 0) {
				for (int i = 0; i < cachetDeptIds.length; i++) {
					Set<String> cachetManageSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(cachetDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getCachetManage())) {
						cachetManageSet.addAll(Arrays.asList(dept.getCachetManage().trim().split(",")));
					}

					cachetManageSet.add(user.getUserId());
					dept.setCachetManage(String.join(",", cachetManageSet));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}

		// 业务分管副总-组织部门
		String[] businessDeptIds = user.getBusinessDeptIds();
		SysDept sysDept3 = new SysDept();
		sysDept3.setDeputyDir(userId);
		List<SysDept> resList3 = sysDeptMapper.selectDeptList(sysDept3);

		if (resList3 == null || resList3.size() == 0) {
			if (businessDeptIds != null && businessDeptIds.length > 0) {
				for (int i = 0; i < businessDeptIds.length; i++) {
					Set<String> deputyDirSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(businessDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getDeputyDir())) {
						deputyDirSet.addAll(Arrays.asList(dept.getDeputyDir().trim().split(",")));
					}

					deputyDirSet.add(user.getUserId());
					dept.setDeputyDir((String.join(",", deputyDirSet)));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		} else {

			for (int i = 0; i < resList3.size(); i++) {
				Set<String> deputyDirSet = new HashSet<>();
				SysDept dept = resList3.get(i);
				if (StringUtils.isNotBlank(dept.getDeputyDir())) {
					deputyDirSet.addAll(Arrays.asList(dept.getDeputyDir().trim().split(",")));
				}

				deputyDirSet.remove(userId);
				dept.setDeputyDir(String.join(",", deputyDirSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (businessDeptIds != null && businessDeptIds.length > 0) {
				for (int i = 0; i < businessDeptIds.length; i++) {
					Set<String> deputyDirSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(businessDeptIds[i]);
					if (StringUtils.isNotBlank(dept.getDeputyDir())) {
						deputyDirSet.addAll(Arrays.asList(dept.getDeputyDir().trim().split(",")));
					}

					deputyDirSet.add(user.getUserId());
					dept.setDeputyDir((String.join(",", deputyDirSet)));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}

		// 部领导-组织部门
		String[] deptLeaderIds = user.getDeptLeaderIds();
		SysDept sysDept4 = new SysDept();
		sysDept4.setLeader(userId);
		List<SysDept> resList4 = sysDeptMapper.selectDeptList(sysDept4);

		if (resList4 == null || resList4.size() == 0) {
			if (deptLeaderIds != null && deptLeaderIds.length > 0) {
				for (int i = 0; i < deptLeaderIds.length; i++) {
					Set<String> leaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
					if (StringUtils.isNotBlank(dept.getLeader())) {
						leaderSet.addAll(Arrays.asList(dept.getLeader().trim().split(",")));
					}

					leaderSet.add(user.getUserId());
					dept.setLeader(((String.join(",", leaderSet))));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		} else {

			for (int i = 0; i < resList4.size(); i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = resList4.get(i);
				if (StringUtils.isNotBlank(dept.getLeader())) {
					leaderSet.addAll(Arrays.asList(dept.getLeader().trim().split(",")));
				}

				leaderSet.remove(userId);
				dept.setLeader(String.join(",", leaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (deptLeaderIds != null && deptLeaderIds.length > 0) {
				for (int i = 0; i < deptLeaderIds.length; i++) {
					Set<String> leaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
					if (StringUtils.isNotBlank(dept.getLeader())) {
						leaderSet.addAll(Arrays.asList(dept.getLeader().trim().split(",")));
					}

					leaderSet.add(user.getUserId());
					dept.setLeader(((String.join(",", leaderSet))));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}

		// 所领导-组织部门
		String[] deptInstituteLeaderIds = user.getDeptInstituteLeaderIds();
		SysDept sysDept41 = new SysDept();
		sysDept41.setWhatBusinessLeader(userId);
		List<SysDept> resList41 = sysDeptMapper.selectDeptList(sysDept41);

		if (resList41 == null || resList41.size() == 0) {
			if (deptInstituteLeaderIds != null && deptInstituteLeaderIds.length > 0) {
				for (int i = 0; i < deptInstituteLeaderIds.length; i++) {
					Set<String> leaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptInstituteLeaderIds[i]);
					if (StringUtils.isNotBlank(dept.getWhatBusinessLeader())) {
						leaderSet.addAll(Arrays.asList(dept.getWhatBusinessLeader().trim().split(",")));
					}

					leaderSet.add(user.getUserId());
					dept.setWhatBusinessLeader(((String.join(",", leaderSet))));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		} else {

			for (int i = 0; i < resList41.size(); i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = resList41.get(i);
				if (StringUtils.isNotBlank(dept.getWhatBusinessLeader())) {
					leaderSet.addAll(Arrays.asList(dept.getWhatBusinessLeader().trim().split(",")));
				}

				leaderSet.remove(userId);
				dept.setWhatBusinessLeader(String.join(",", leaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}

			if (deptInstituteLeaderIds != null && deptInstituteLeaderIds.length > 0) {
				for (int i = 0; i < deptInstituteLeaderIds.length; i++) {
					Set<String> leaderSet = new HashSet<>();
					SysDept dept = sysDeptMapper.selectDeptById(deptInstituteLeaderIds[i]);
					if (StringUtils.isNotBlank(dept.getWhatBusinessLeader())) {
						leaderSet.addAll(Arrays.asList(dept.getWhatBusinessLeader().trim().split(",")));
					}

					leaderSet.add(user.getUserId());
					dept.setWhatBusinessLeader(((String.join(",", leaderSet))));
					dept.setUpdateBy(updateBy);
					sysDeptMapper.updateDept(dept);
				}
			}
		}
		updateBpmUserStatus(user,"2");
		SysUser newUser = userMapper.selectUserById(user.getUserId());
		ToPortalUserInfo toPortalUserInfo = new ToPortalUserInfo();
		toPortalUserInfo.setFlag("2");
		toPortalUserInfo.setUserNo(user.getJobNo());
		toPortalUserInfo.setUserName(user.getNickName());
		toPortalUserInfo.setCompetentOrg(user.getPersonnelDept());
		if(StringUtils.isNotBlank(user.getStatus())) {
			String status = user.getStatus();
			if("0".equals(status)) {
				toPortalUserInfo.setUserStatus("1");
			}else if("1".equals(status)) {
				toPortalUserInfo.setUserStatus("2");
			}else if("2".equals(status)) {
				toPortalUserInfo.setUserStatus("3");
			}
		}
		if(newUser.getLoginDate()!=null && !"".equals(newUser.getLoginDate().toString())) {
			SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
			sdf.format(newUser.getLoginDate());
			toPortalUserInfo.setLoginDate(newUser.getLoginDate().toString());
		}
		
		toPortalUserInfo.setLoginName(newUser.getUserName());
		pushToPortalUserInfoService.sendUserInfo(toPortalUserInfo);
		
		return userMapper.updateUser(user);
	}

	/**
	 * 修改用户状态
	 *
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int updateUserStatus(SysUser user) {
		return userMapper.updateUser(user);
	}

	/**
	 * 修改用户基本信息
	 *
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int updateUserProfile(SysUser user) {
		return userMapper.updateUser(user);
	}

	/**
	 * 修改用户头像
	 *
	 * @param userName 用户ID
	 * @param avatar   头像地址
	 * @return 结果
	 */
	public boolean updateUserAvatar(String userName, String avatar) {
		return userMapper.updateUserAvatar(userName, avatar) > 0;
	}

	/**
	 * 重置用户密码
	 *
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int resetPwd(SysUser user) {
		return userMapper.updateUser(user);
	}

	/**
	 * 重置用户密码
	 *
	 * @param userName 用户名
	 * @param password 密码
	 * @return 结果
	 */
	@Override
	public int resetUserPwd(String userName, String password) {
		return userMapper.resetUserPwd(userName, password);
	}

	/**
	 * 新增用户角色信息
	 *
	 * @param user 用户对象
	 */
	public void insertUserRole(SysUser user) {
		String[] roles = user.getRoleIds();
		roles = insertGeneralRole(roles);
		if (StringUtils.isNotNull(roles)) {
			// 新增用户与角色管理
			List<SysUserRole> list = new ArrayList<SysUserRole>();
			for (String roleId : roles) {
				SysUserRole ur = new SysUserRole();
				ur.setUserId(user.getUserId());
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleMapper.batchUserRole(list);
			}
		}
	}

	/**
	 * 添加普通户用权限
	 */
	private String[] insertGeneralRole(String[] roles) {
		Set<String> roleSet = new HashSet<>();
		if(roles!=null&&roles.length>0){
			roleSet = new HashSet<>(Arrays.asList(roles));
		}
		// 添加普通用户角色,通过set转换，过滤掉相同角色添加多次的问题
		SysRole role = roleMapper.selectRoleByRoleKey(Constants.ROLE_GENERAL_USER_KEY);
		roleSet.add(role.getRoleId());
		return roleSet.toArray(new String[roleSet.size()]);
	}

	/**
	 * 新增用户岗位信息
	 *
	 * @param user 用户对象
	 */
	public void insertUserPost(SysUser user) {
		String[] posts = user.getPostIds();
		if (StringUtils.isNotNull(posts)) {
			// 新增用户与岗位管理
			List<SysUserPost> list = new ArrayList<SysUserPost>();
			for (String postId : posts) {
				SysUserPost up = new SysUserPost();
				up.setUserId(user.getUserId());
				up.setPostId(postId);
				list.add(up);
			}
			if (list.size() > 0) {
				userPostMapper.batchUserPost(list);
			}
		}
	}

	/**
	 * 通过用户ID删除用户
	 *
	 * @param userId 用户ID
	 * @return 结果
	 */
	@Override
	public int deleteUserById(String userId) {
		// 删除用户与角色关联
		userRoleMapper.deleteUserRoleByUserId(userId);
		// 删除用户与岗位表
		userPostMapper.deleteUserPostByUserId(userId);
		return userMapper.deleteUserById(userId);
	}

	/**
	 * 批量删除用户信息
	 *
	 * @param userIds 需要删除的用户ID
	 * @return 结果
	 */
	public int deleteUserByIds(String[] userIds) {
		for (String userId : userIds) {
			checkUserAllowed(new SysUser(userId));
		}
		return userMapper.deleteUserByIds(userIds);
	}

	/**
	 * 导入用户数据
	 *
	 * @param userList        用户数据列表
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 * @param operName        操作用户
	 * @return 结果
	 */
	@Override
	public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
		if (StringUtils.isNull(userList) || userList.size() == 0) {
			throw new CustomException("导入用户数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		String password = configService.selectConfigByKey("sys.user.initPassword");
		for (SysUser user : userList) {
			try {
				// 验证是否存在这个用户
				SysUser u = userMapper.selectUserByUserName(user.getUserName());
				if (StringUtils.isNull(u)) {
					user.setPassword(SecurityUtils.encryptPassword(password));
					user.setCreateBy(operName);
					this.insertUser(user);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
				} else if (isUpdateSupport) {
					user.setUpdateBy(operName);
					this.updateUser(user, false);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
				} else {
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
				}
			} catch (Exception e) {
				failureNum++;
				String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new CustomException(failureMsg.toString());
		} else {
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	@Override
	public int editUserPwd(String phonenumber, String password, String code, String uuid) {
		String verifyKey = Constants.CAPTCHA_CODE_KEY + phonenumber + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		if (!code.equalsIgnoreCase(captcha)) {
			AsyncManager.me().execute(AsyncFactory.recordLogininfor(phonenumber, Constants.LOGIN_FAIL,
					MessageUtils.message("user.jcaptcha.error")));
			throw new CaptchaException();
		}
		if (StringUtils.isNotBlank(phonenumber) && StringUtils.isNotBlank(password)) {
			SysUser user = new SysUser();
			user.setPassword(SecurityUtils.encryptPassword(password));
			user.setPhonenumber(phonenumber);
			SysUser resultUser = userMapper.selectUserByPhonenumber(phonenumber);
			if(tokenService.getLoginUser(ServletUtils.getRequest())==null) {
				user.setUpdateBy(resultUser.getUserId());
			}else {
				user.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
			}
			
			return userMapper.resetUserPwdByPhonenumber(user);
		} else {
			return 0;
		}
	}

	/**
	 * 是否包含某个角色
	 * 
	 * @param uuid
	 * @param roleName
	 * @return
	 */
	@Override
	public boolean isContainRole(String uuid, String roleName) {
		boolean isContainRole = false;
		if (StringUtils.isAnyBlank(uuid, roleName)) {
			return isContainRole;
		}
		SysUser sysUser = selectUserById(uuid);
		if (null == sysUser) {
			return isContainRole;
		}
		List<SysRole> roles = sysUser.getRoles();
		for (SysRole role : roles) {
			if (role.getRoleName().equals(roleName)) {
				isContainRole = true;
				break;
			} else {
				continue;
			}
		}
		return isContainRole;
	}

	/**
	 * 通过项目负责人代码到项目负责人表（BmsProjectLeader）找到对应项目负责人，
	 * 再通过项目负责人的字段userGlobalid（用户表globalld）， 去找到我们系统用户表Sys_user找到对应项目负责人
	 * 
	 * @param projectLeaderNo 项目负责人代码
	 * @return
	 */
	@Override
	public SysUser findSysuUserByProjectLeaderNo(String projectLeaderNo) {
		return userMapper.findSysuUserByProjectLeaderNo(projectLeaderNo);
	}

	/**
	 * 通过用户guid查找项目负责人表项目代码
	 * @param userGuid 用户guid
	 * @return
	 */
	@Override
	public String findProjectLeaderNoByUserGuid(String userGuid) {
		return userMapper.findProjectLeaderNoByUserGuid(userGuid);
	}

	@Override
	public String checkJobNoUnique(SysUser user) {
		String userId = StringUtils.isNull(user.getUserId()) ? "-1L" : user.getUserId();
		SysUser info = userMapper.checkJobNoUnique(user.getJobNo());
		if (StringUtils.isNotNull(info) && !userId.equals(info.getUserId())) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public SysUser selectUserByJobNo(String jobNo) {
		return userMapper.selectUserByJobNo(jobNo);
	}

	@Override
	public int codeVerify(String code, String phonenumber, String uuid) {
		String verifyKey = Constants.CAPTCHA_CODE_KEY + phonenumber + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		if (!code.equalsIgnoreCase(captcha)) {
			AsyncManager.me().execute(AsyncFactory.recordLogininfor(phonenumber, Constants.LOGIN_FAIL,
					MessageUtils.message("user.jcaptcha.error")));
			throw new CaptchaException();
		}
		return 1;
	}

	/**
	 * 根据ec用户状态修改bpm用户状态
	 */
	public void updateBpmUserStatus(SysUser user,String flag){
		Map<String,String> map = new HashMap<String,String>();
		String state = "0";
		//用户状态
		String status = user.getStatus();
		map.put("userNo",user.getJobNo());
		if("2".equals(flag)) {
			Map<String, Object> portalRes = ThreadPost.doPost2Request(portalBaseUrl+userStatusUrl,  JSONUtils.toJson(map), null);
			if(portalRes!=null && portalRes.get("data")!=null) {
				String portalStatus = portalRes.get("data").toString();
				if("1".equals(status) && "1".equals(portalStatus)) {
					state = "0";
				}else {
					state = "1";
				}
			}else {
				if("0".equals(status)) {//正常
					state = "1";
				}else if("1".equals(status)) {//停用
					state = "0";
				}else {
					state = "0";
				}
			}
		}else {
			if("0".equals(status)) {//正常
				state = "1";
			}else if("1".equals(status)) {//停用
				state = "0";
			}else {
				state = "0";
			}
		}
		
		map.put("state", state);
		//用户bpmid
		String bpmUserId = user.getBpmUserId();
		map.put("objectId", bpmUserId);
		
		log.info("修改用户的信息", "工号="+user.getJobNo()+",用户状态="+user.getStatus());
		
		
		
		Map<String, Object> requestString = ThreadPost.doPost2Request(baseUrl+url, JSONUtils.toJson(map), null);
		log.info("修改bpm用户状态结果", requestString);
		
	}

	@Override
	public int insertUserLeader(SysUser user) {

		List<BmsProjectLeader> result = bmsProjectLeaderMapper.selectBmsProjectLeaderByUserglobalId(user.getGlobalId());
		if (!CollectionUtils.isEmpty(result)) {
			SysRole role = roleMapper.checkRoleKeyUnique("project_leader");
			String[] roleIds = user.getRoleIds();
            int size = 0;
            if(roleIds!=null&&roleIds.length>0){
                size = roleIds.length;
            }
			String[] tmp = new String[size + 1];
			for (int i = 0; i < size; i++) {
				tmp[i] = roleIds[i];
			}
			tmp[size] = role.getRoleId();

			user.setRoleIds(tmp);
		}

		String updateBy = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
		// 新增用户信息
		int rows = userMapper.insertUser(user);
		user.setUserId(user.getGuid());
		// 新增用户岗位关联
		insertUserPost(user);
		// 新增用户与角色管理,默认赋予用户普通角色权利
		insertUserRole(user);
		//如果为项目负责人，添加自己到人员清单表中
		if (!CollectionUtils.isEmpty(result)) {
			BmsLeaderMember newBmsLeaderMember = new BmsLeaderMember();
			newBmsLeaderMember.setLeader(user.getGuid());
			newBmsLeaderMember.setUserId(user.getGuid());
			List<BmsLeaderMember> bmsLeaderMembers = bmsLeaderMemberService.selectBmsLeaderMemberList(newBmsLeaderMember);
			if (CollectionUtils.isEmpty(bmsLeaderMembers)) {
				bmsLeaderMemberService.insertBmsLeaderMember(newBmsLeaderMember);
			}
		}
		// 部门技术负责人-组织部门
		String[] deptIds = user.getDeptIds();
		if (deptIds != null && deptIds.length > 0) {
			for (int i = 0; i < deptIds.length; i++) {
				Set<String> techLeaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
				techLeaderSet.addAll(Arrays.asList(dept.getTechLeader().trim().split(",")));
				techLeaderSet.add(user.getUserId());
				dept.setTechLeader(String.join(",", techLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 所技术负责人-组织部门
		String[] instituteDeptIds = user.getInstituteDeptIds();
		if (instituteDeptIds != null && instituteDeptIds.length > 0) {
			for (int i = 0; i < instituteDeptIds.length; i++) {
				Set<String> techLeaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptIds[i]);
				techLeaderSet.addAll(Arrays.asList(dept.getWhatBusinessTechLeader().trim().split(",")));
				techLeaderSet.add(user.getUserId());
				dept.setWhatBusinessTechLeader(String.join(",", techLeaderSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 专业技术负责人--业务分管类别
		String[] businessTypeIds = user.getBusinessTypeIds();
		if (businessTypeIds != null && businessTypeIds.length > 0) {
			List<BmsUserBusiness> bmsUserBusinesss = new ArrayList<BmsUserBusiness>();
			for (int i = 0; i < businessTypeIds.length; i++) {
				BmsUserBusiness bmsUserBusiness = new BmsUserBusiness();
				bmsUserBusiness.setUserId(user.getUserId());
				bmsUserBusiness.setBusinessId(businessTypeIds[i]);
				bmsUserBusinesss.add(bmsUserBusiness);
			}
			bmsUserBusinessMapper.batchUserBusiness(bmsUserBusinesss);
		}

		// 公章管理员-组织部门
		String[] cachetDeptIds = user.getCachetDeptIds();
		if (cachetDeptIds != null && cachetDeptIds.length > 0) {
			for (int i = 0; i < cachetDeptIds.length; i++) {
				Set<String> cachetManageSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(cachetDeptIds[i]);
				cachetManageSet.addAll(Arrays.asList(dept.getCachetManage().trim().split(",")));
				cachetManageSet.add(user.getUserId());
				dept.setCachetManage(String.join(",", cachetManageSet));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 业务分管副总-组织部门
		String[] businessDeptIds = user.getBusinessDeptIds();
		if (businessDeptIds != null && businessDeptIds.length > 0) {
			for (int i = 0; i < businessDeptIds.length; i++) {
				Set<String> deputyDirSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(businessDeptIds[i]);
				deputyDirSet.addAll(Arrays.asList(dept.getDeputyDir().trim().split(",")));
				deputyDirSet.add(user.getUserId());
				dept.setDeputyDir((String.join(",", deputyDirSet)));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 部领导-组织部门
		String[] deptLeaderIds = user.getDeptLeaderIds();
		if (deptLeaderIds != null && deptLeaderIds.length > 0) {
			for (int i = 0; i < deptLeaderIds.length; i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
				leaderSet.addAll(Arrays.asList(dept.getLeader().trim().split(",")));
				leaderSet.add(user.getUserId());
				dept.setLeader(((String.join(",", leaderSet))));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}

		// 所领导-组织部门
		String[] deptInstituteLeaderIds = user.getDeptInstituteLeaderIds();
		if (deptInstituteLeaderIds != null && deptInstituteLeaderIds.length > 0) {
			for (int i = 0; i < deptInstituteLeaderIds.length; i++) {
				Set<String> leaderSet = new HashSet<>();
				SysDept dept = sysDeptMapper.selectDeptById(deptLeaderIds[i]);
				leaderSet.addAll(Arrays.asList(dept.getWhatBusinessLeader().trim().split(",")));
				leaderSet.add(user.getUserId());
				dept.setWhatBusinessLeader(((String.join(",", leaderSet))));
				dept.setUpdateBy(updateBy);
				sysDeptMapper.updateDept(dept);
			}
		}
		return rows;
	}

	@Override
	public int updatePersonnelDept(String newPersonnelDept, String oldPersonnelDept) {
		log.info("新部门名称==="+newPersonnelDept);
		log.info("老部门名称==="+oldPersonnelDept);
		return userMapper.updatePersonnelDept(newPersonnelDept, oldPersonnelDept);
	}

	@Override
	public int updatePersonnelDeptByJobNo(String newPersonnelDept, String jobNo) {
		log.info("新部门名称==="+newPersonnelDept);
		log.info("工号==="+jobNo);
		return userMapper.updatePersonnelDeptByJobNo(newPersonnelDept, jobNo);
	}

	@Override
	public String selectUserJobNoByGuid(String guid) {
		log.info("人员guid==="+guid);
		return userMapper.selectUserJobNoByGuid(guid);
	}

}
